const {Config} = require("./config");
const {store} = require("./config");
const {app} = require("electron")

/**
 * 开启开机启动
 */
function enableAutoStart() {
    let isOpen = store.get(Config.enableAutoStartKEY, false);
    if (!isOpen && app.isPackaged) {
        app.setLoginItemSettings({
            openAtLogin: true,
            openAsHidden: true,
            enabled: true,
            args: ["--openAsHidden", "yes"],
        })
        store.set(Config.enableAutoStartKEY, true)
    }
}

exports.enableAutoStart = enableAutoStart;

/**
 * 禁用开机启动
 */
function disableAutoStart() {
    let isOpen = store.get(Config.enableAutoStartKEY, false);
    if (app.isPackaged && isOpen) {
        app.setLoginItemSettings({
            openAtLogin: true,
            openAsHidden: true,
            enabled: false,
        })
        store.set(Config.enableAutoStartKEY, false)
    }
}

exports.disableAutoStart = disableAutoStart

/**
 * 本启动是否是开机自启动
 * @return boolean
 */
function getOpenFormLogin() {
    if (process.platform === "darwin") {
        return app.getLoginItemSettings().wasOpenedAtLogin;
    } else {
        return process.argv.indexOf("--openAsHidden") > -1;
    }
}

exports.getOpenFormLogin = getOpenFormLogin